<?php
namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;

/**
 * This is the model class for table "game_notice".
 *
 * @property integer $id
 * @property integer $showchannel
 * @property integer $gametype
 * @property integer $game_id
 * @property string $game_name
 * @property integer $starttime
 * @property integer $endtime
 * @property string $title
 * @property string $url
 * @property integer $orderno
 * @property integer $created_at
 * @property integer $updated_at
 */
class GameNotice extends \yii\db\ActiveRecord
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'game_notice';
    }

    /*
     * 产生创建日期和更新日期时间戳
     */
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'createdAtAttribute' => 'created_at',
                'updatedAtAttribute' => 'updated_at'
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [
                [
                    'id',
                    'showchannel',
                    'gametype',
                    'game_id',
                    'starttime',
                    'endtime',
                    'orderno',
                    'created_at',
                    'updated_at'
                ],
                'integer'
            ],
            [
                [
                    'game_name'
                ],
                'string',
                'max' => 100
            ],
            [
                [
                    'title',
                    'url'
                ],
                'string',
                'max' => 200
            ],
            [
                'url',
                'match',
                'pattern' => "/\b(?:(?:https?):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",
                'message' => '链接URL不是一个有效的URL.'
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'showchannel' => '显示',
            'gametype' => '0:不关联游戏 1：单机，2：网游',
            'game_id' => '游戏ID',
            'game_name' => '游戏名称',
            'starttime' => '开始显示时间',
            'endtime' => '结束显示时间',
            'title' => '标题',
            'url' => '链接',
            'orderno' => '显示位置，小的显示前面',
            'created_at' => '创建时间',
            'updated_at' => '更新时间'
        ];
    }

    // relationship
    /**
     * 所属游戏
     */
    public function getGame()
    {
        return $this->hasOne(Game::className(), [
            'game_id' => 'game_id'
        ]);
    }

    /**
     * 所属游戏信息
     */
    public function getGameInfo()
    {
        return $this->hasOne(GameInfo::className(), [
            'game_id' => 'game_id'
        ]);
    }
}
